/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: DuKe TeAm
 * License Type: Purchased
 */
package smartgrid;

import org.orm.PersistentException;
public class TariffProcessor {
	private int ID;
	
	private String Name;
	
	private String action="";
	
	public void setID(int value) {
		this.ID = value;
	}
	
	public int getID() {
		return ID;
	}
	
	/**
	 * Nome do tarifário.
	 */
	public void setName(String value) {
		this.Name = value;
	}
	
	public String getName() {
		return Name == null ? "" : Name;
	}
	
	public String getAction() {
		return action;
	}
	
	public void setAction(String action) {
		this.action = action;
	}
	
	private int Supplier_Tariffs_role_supplierID;
	
	public void setSupplier_Tariffs_role_supplierID(int value) {
		this.Supplier_Tariffs_role_supplierID = value;
	}
	
	public int getSupplier_Tariffs_role_supplierID() {
		return Supplier_Tariffs_role_supplierID;
	}
	
	public String process() {
		String result = "Unexcepted result";
		if (action.equals("search")) {
			smartgrid.Tariff _tariff = smartgrid.Tariff.loadTariffByORMID(getID());
			if (_tariff!= null) {
				copyFromBean(_tariff);
				result = "Search success";
			}
			else {
				result = "Search failed";
			}
		}
		else if(action.equals("insert"))  {
			try {
				smartgrid.Tariff _tariff = smartgrid.Tariff.createTariff();
				copyToBean(_tariff);
				if (_tariff.save()) {
					result = "Insert success";
				}
				else {
					result = "Insert failed";
				}
			}
			catch (Exception e) {
				result = "Insert error: " + e.toString();
			}
		}
		else if (action.equals("update")) {
			smartgrid.Tariff _tariff= smartgrid.Tariff.loadTariffByORMID(getID());
			if (_tariff != null) {
				copyToBean(_tariff);
				if (_tariff.save()) {
					result = "Update success";
				}
				 else {
					result = "Update failed";
				}
			}
			 else  {
				result = "Update failed";
			}
			
		}
		else if (action.equals("delete")) {
			smartgrid.Tariff _tariff = smartgrid.Tariff.loadTariffByORMID(getID());
			if (_tariff != null && _tariff.deleteAndDissociate()) {
				result = "Delete success";
			}
			else {
				result = "Delete failed";
			}
		}
		else if (action.equals("")) {
			result = "";
		}
		action = "";
		return result;
	}
	
	private void copyFromBean(smartgrid.Tariff _tariff) {
		setName(_tariff.getName());
		setID(_tariff.getORMID());
		
		{
			smartgrid.Supplier _supplier = _tariff.getSupplier_Tariffs_role();
			if (_supplier != null) {
				setSupplier_Tariffs_role_supplierID(_supplier.getORMID());
			}
		}
		
	}
	
	private void copyToBean(smartgrid.Tariff _tariff) {
		_tariff.setName(getName());
		smartgrid.Supplier _supplier_Tariffs_role = smartgrid.Supplier.loadSupplierByORMID(getSupplier_Tariffs_role_supplierID());
		_tariff.setSupplier_Tariffs_role(_supplier_Tariffs_role);
	}
	
}

